﻿
using Butcher.winform.Dto;
using Newtonsoft.Json.Linq;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.Domains.BatchManagementFiles;
using Traceability.Commont.Domains.ScratchTheFat;
using Traceability.Commont.DTOs.BatchManagementDTOs;
using static Butcher.winform.EnterpriseForms.DeductionHtml;
using static Butcher.winform.EnterpriseForms.MassSlaughter;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Butcher.winform.EnterpriseForms
{
    public partial class WhiteNoteList : Form
    {
        Scale ScaleInfo = new Scale();
        public WhiteNoteList()
        {
            InitializeComponent();
            WhiteHtml();
            tb_GrossWeight.LostFocus += new EventHandler(textBox2_GotFocus);
            var url = @"http://localhost:5229/api/Batch/WhiteBulkList";

            HttpClient client = new HttpClient();
            NEWPUT nEWPUT = new NEWPUT();
            var strValue = ScaleBackfill.TxtVal;
            if (!string.IsNullOrEmpty(strValue))
            {
                ScaleInfo = Json.Deserialize<Scale>(strValue);
                ui_tare.Text = ScaleInfo.TareWeightDeduction.ToString();
                getStash(ScaleInfo.Warehouse);
            }
            var content = new StringContent(JsonSerializer.Serialize<NEWPUT>(nEWPUT));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var count = info["date"].Value<JArray>();
            List<BuclkWeight> enterprises = count.ToObject<List<BuclkWeight>>();
            // "slaughterNumber": "123123",
            //       "bulkSlaughterNum": 10,
            // "weightNum": 0,
            //"num": 10


            for (var i = 0; i < enterprises.Count; i++)
            {
                UIFlowLayoutPanel list = new UIFlowLayoutPanel();
                UIButton newgetbutton = new UIButton();

                newgetbutton.Location = new Point(10, 10);
                newgetbutton.Click += new EventHandler(MyButton_Click);
                newgetbutton.Text = "选择" + "" + i + "";
                newgetbutton.Name = i.ToString();

                list.Width = 140;
                list.Height = 180;
                for (var j = 0; j < 3; j++)
                {
                    Label label = new Label();
                    Label labe2 = new Label();
                    label.Width = 50;
                    label.Height = 50;
                    labe2.Width = 80;
                    labe2.Height = 50;
                    if (j == 0)
                    {
                        labe2.Text = "屠宰编号";
                        label.Text = enterprises[i].SlaughterNumber;
                    }
                    else if (j == 1)
                    {
                        labe2.Text = "计划";
                        label.Text = Convert.ToString(enterprises[i].BulkSlaughterNum);

                    }
                    else if (j == 2)
                    {
                        labe2.Text = "称重";
                        label.Text = Convert.ToString(enterprises[i].WeightNum);
                    }
                    list.Controls.Add(labe2);
                    list.Controls.Add(label);
                }
                list.Controls.Add(newgetbutton);
                uiFlowLayoutPanel1.Controls.Add(list);
            }
        }
        public void textBox2_GotFocus(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ui_tare.Text) && !string.IsNullOrEmpty(tb_GrossWeight.Text))
            {
                ui_NetWeight.Text = (Convert.ToDecimal(tb_GrossWeight.Text) - Convert.ToDecimal(ui_tare.Text)).ToString();
            }
        }


        public static int ids;

        private void WhiteNoteList_Load(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var l = e.ColumnIndex;
            if (l == 1)
            {
                DataGridViewRow row = dataGridView1.Rows[e.ColumnIndex];
                Putid list = new Putid();
                list.WihteWeightid = Convert.ToInt32(row.Cells["序号"].Value);
                HttpClient client = new HttpClient();
                string url = @" http://localhost:5229/api/Batch/WhiteStripWeighingDel";
                var content = new StringContent(JsonSerializer.Serialize<Putid>(list), Encoding.UTF8);
                content.Headers.Remove("Content-Type");
                content.Headers.Add("Content-Type", "application/json");
                string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
                JObject josnresult = JObject.Parse(result);
                var results = josnresult["code"];


                if (results != null)
                {
                    MessageBox.Show("删除成功");
                    WhiteHtml();
                }
            }
            else if (l == 0)
            {
                DataGridViewRow row = dataGridView1.Rows[e.ColumnIndex];

                ids = Convert.ToInt32(row.Cells["序号"].Value);
                WhiteNoteUpdate gethtml = new WhiteNoteUpdate();

                gethtml.Show();

            }
        }

        public void WhiteHtml()
        {

            string url = @"http://localhost:5229/api/Batch/WhiteStripWeighingView";
            HttpClient client = new HttpClient();
            NEWGroup list = new NEWGroup();
            list.pageindex = 1;
            list.pagesize = 10;
            var content = new StringContent(JsonSerializer.Serialize<NEWGroup>(list), Encoding.UTF8);
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject josnresult = JObject.Parse(result);
            var results = josnresult["date"].Value<JArray>();

            dataGridView1.DataSource = results;

        }


        public class NEWGroup
        {
            public int pageindex { get; set; }
            public int pagesize { get; set; }
        }
        /// <summary>
        /// 磅秤显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiSymbolButton2_Click(object sender, EventArgs e)
        {

            ScaleBackfill.pid = 2;
            ScaleBackfill list = new ScaleBackfill();

            list.Show();


        }


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>

        private void button24_Click(object sender, EventArgs e)
        {
            string url = @"http://localhost:5229/api/Batch/WhiteStripWeighingCreate";
            HttpClient client = new HttpClient();
            WhiteNotes list = new WhiteNotes();
            list.Stashid = Convert.ToInt32(cb_Level.SelectedValue);
            list.ShoppingDesignationid = 4;
            list.WhiteNoteNum = Convert.ToInt32(uiTextBox1.Text);
            list.WhiteNoteWeight = Convert.ToDecimal(tb_GrossWeight.Text);
            list.WhiteNoteTare = Convert.ToDecimal(ui_tare.Text);
            list.NetWeight = Convert.ToDecimal(tb_GrossWeight.Text) - Convert.ToDecimal(ui_tare.Text);
            list.ScratchTheFat = "大划";
            list.Ductionid = 1;
            list.WhiteNoteRemark = textBox7.Text;
            list.SlaughterNumber = "123123";
            var content = new StringContent(JsonSerializer.Serialize<WhiteNotes>(list), Encoding.UTF8);
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject josnresult = JObject.Parse(result);
            var results = josnresult["code"].Value<int>();

            if (Convert.ToInt32(results) == 1)
            {



                string url1 = @"http://localhost:5229/api/Batch/LoginCreate";
                HttpClient client1 = new HttpClient();
                NewLogions list1 = new NewLogions();
                list1.ControlsTime = DateTime.Now;
                list1.ControlsName = "admin";
                list1.Loginid = 0;
                list1.WhatControls = "白条添加";
                var content1 = new StringContent(JsonSerializer.Serialize<NewLogions>(list1), Encoding.UTF8);
                content1.Headers.Remove("Content-Type");
                content1.Headers.Add("Content-Type", "application/json");
                string result1 = client1.PostAsync(url1, content1).Result.Content.ReadAsStringAsync().Result;
                JObject josnresult1 = JObject.Parse(result1);
                var results1 = josnresult1["code"].Value<int>();


                MessageBox.Show("添加成功");







            }
        }



        public class Putid
        {
            public int WihteWeightid
            {
                get; set;

            }
        }




        private void MyButton_Click(object sender, EventArgs e)
        {
            UIButton button = sender as UIButton;
            int gh = 0;
            if (button != null)
            {
                gh = Convert.ToInt32(button.Name);
            }

            var url = @"http://localhost:5229/api/Batch/WhiteBulkList";

            HttpClient client = new HttpClient();
            NEWPUT nEWPUT = new NEWPUT();

            var content = new StringContent(JsonSerializer.Serialize<NEWPUT>(nEWPUT));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var count = info["date"].Value<JArray>();
            List<BuclkWeight> enterprises = count.ToObject<List<BuclkWeight>>();

            var hk = 0;
            for (var i = 0; i < enterprises.Count; i++)
            {
                hk = Convert.ToInt32(enterprises[gh].SlaughterNumber);



            }

            MessageBox.Show("ok" + hk);

        }

        public void getStash(int id)
        {
            Stash stash = new Stash();
            string url = @"http://localhost:5229/api/Batch/StashList";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<Stash>(stash));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var saaa = result;
            var aaa = info["date"].Value<JArray>();
            List<Stash> enterprises = aaa.ToObject<List<Stash>>();
            enterprises = enterprises.Where(x => x.Stashid == id).ToList();
            this.cb_Level.DataSource = enterprises;
            this.cb_Level.DisplayMember = "StashName";
            this.cb_Level.ValueMember = "Stashid";
        }

        private void uiSymbolButton1_Click(object sender, EventArgs e)
        {
            ScaleBackfill.pid = 2;
            ScaleBackfill scale = new ScaleBackfill();
            scale.Owner = this;
            scale.Hide();
            scale.ShowDialog();
            Application.ExitThread();
        }
    }
}
